package com.atguigu.yygh.msm.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.msm.service.MsmService;
import com.atguigu.yygh.msm.utils.RandomUtil;
import com.atguigu.yygh.vo.msm.MsmVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(String phone, Map<String, Object> params) {
        //1判断手机号是否为空
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        //2创建客户端对象
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI5tJGivF3UM883gmBRKe6", "UvjKguPc7Izq3smlZhaHN0zd4jViDh");
        IAcsClient client = new DefaultAcsClient(profile);
        //3创建请求对象，设置参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "佫亚强的短信服务");
        request.putQueryParameter("TemplateCode", "SMS_272485961");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(params));
        try {
            //4客户端调用方法发送请求，获取响应
            CommonResponse response = client.getCommonResponse(request);
            //5从响应中获得结果返回
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();

        } catch (ClientException e) {
            e.printStackTrace();
            throw new YyghException(20001, "发送短信失败");
        }
    }
    //发送短信接口
    @Override
    public boolean send(MsmVo msmVo) {
        //1、获取参数
        String phone = msmVo.getPhone();
        String templateCode = msmVo.getTemplateCode();
        Map<String, Object> param = msmVo.getParam();
        //2、模拟发送通知短信
        String code = RandomUtil.getFourBitRandom();
        Map<String,Object> param1 = new HashMap<>();
        param1.put("code",code);
        //4调用方法发送验证码短信
        //boolean isSend = this.send(phone,param1);
          boolean isSend = true;

        return isSend;
    }


}
